#include<iostream>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
 
class Solution {
private:
    void helper(TreeNode* root, int max_,int& res) {
        if (root == NULL) return;
        if (root->val >= max_) {
            ++res;
            max_ = root->val;
        }
        helper(root->left, max_, res);
        helper(root->right, max_, res);

    }


public:
    int goodNodes(TreeNode* root) {
        int max_ = INT32_MIN;
        int res = 0;
        helper(root, max_, res);
        return res;
    }
};